#delegate show
from PyQt5.QtWidgets import QAbstractItemView,QStyleOptionViewItem, QWidget, QApplication, QTableWidget, QTableWidgetItem, QItemEditorCreatorBase, QDateTimeEdit, QItemEditorFactory, QStyledItemDelegate
from PyQt5.QtCore import QVariant, QDateTime, QObject,Qt,QModelIndex,pyqtSlot,pyqtSignal,QAbstractItemModel
import sys
class SpreadSheetDelegate(QStyledItemDelegate):
    def __init__(self, parent = None) -> None:
        super().__init__(parent)

    def create_editor(self, parent: QWidget,
                      option: QStyleOptionViewItem,
                      index: QModelIndex) -> QWidget:
        print("here")
        if index.column() == 1:
            editor = QDateTimeEdit(parent)
            editor.setDisplayFormat("dd/M/yyyy")
            editor.setCalendarPopup(True)
            return editor

        editor = QLineEdit(parent)

        # create a completer with the strings in the column as model
        allStrings = QStringListModel()
        print(index.model())
        for i in range(1, index.model().rowCount()):
            strItem = str(index.model().data(index.sibling(i, index.column()), Qt.EditRole))

            if not allStrings.contains(strItem):
                allStrings.append(strItem)

        autoComplete = QCompleter(allStrings)
        editor.setCompleter(autoComplete)
        editor.editingFinished.connect(SpreadSheetDelegate.commit_and_close_editor)
        return editor

    @pyqtSlot()
    def commit_and_close_editor(self) -> None:
        editor = self.sender()
        self.commitData.emit(editor)
        self.closeEditor.emit(editor)

    def set_editor_data(self, editor: QWidget, index: QModelIndex) -> None:
        logging.info("set_editor_data")
        edit = QLineEdit(editor)
        if edit:
            edit.setText(str(index.model().data(index, Qt.EditRole)))
            return

        dateEditor = QDateTimeEdit(editor)
        if dateEditor:
            dateEditor.setDate(
                QDate.fromString(
                    str(index.model().data(index, Qt.EditRole)), "d/M/yyyy"))

    def set_model_data(self, editor: QWidget,
                       model: QAbstractItemModel, index: QModelIndex) -> None:
        edit = QLineEdit(editor)
        if edit:
            model.setData(index, edit.text())
            return

        dateEditor = QDateTimeEdit(editor)
        if dateEditor:
            model.setData(index, dateEditor.date().toString("dd/M/yyyy"))
class DateTimeEditorCreator(QItemEditorCreatorBase):
    """
    See gui/itemviews/qitemeditorfactory.cpp for implementations of
    createEditor() and valuePropertyName()
    """
    def __init__(self):
        QItemEditorCreatorBase.__init__(self)

    def createWidget(self, parent):
        wid = QDateTimeEdit(parent)
        wid.setCalendarPopup(True)
        wid.setFrame(False)
        return wid

    def valuePropertyName(self):
        return "dateTime"

def die():
    raise MyError("Oops")

class MyError(Exception):
    def __init__(self, value):
        self.value = value
    def __str__(self):
        return repr(self.value)
app = QApplication(sys.argv)
tableWidget = QTableWidget(12, 3)
# tableWidget.setItemDelegate(QStyledItemDelegate())
# tableWidget.itemDelegate().setItemEditorFactory(QItemEditorFactory())
# tableWidget.itemDelegate().itemEditorFactory().registerEditor(QVariant.DateTime, DateTimeEditorCreator())
tableWidget.setItemDelegate(SpreadSheetDelegate())
tableWidget.setEditTriggers(
            QAbstractItemView.DoubleClicked | QAbstractItemView.SelectedClicked)
for row in range(10):
    for col in range(3):
        date = QDateTime.currentDateTime()
        string = str(row) + ',' + str(col)
        item = QTableWidgetItem()
        item.setData(Qt.DisplayRole, QVariant(date))
        tableWidget.setItem(row, col, item)
tableWidget.show()
sys.exit(app.exec_())